package com.imooc.bilibili.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.bilibili.domain.core.JsonResponse;
import com.imooc.bilibili.domain.video.*;
import com.imooc.bilibili.elasticsearch.ElasticSearchService;
import com.imooc.bilibili.support.UserSupport;
import com.imooc.bilibili.video.IVideoService;
import org.apache.mahout.cf.taste.common.TasteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 视频Api
 *
 * 视频安全方面
 *  防止别人调用，下载等等
 * 后端接口包一层
 *  后端进行加密
 *  前端进行解密
 * @author gcq
 * @Create 2022-06-04
 */
@RestController
public class VideoApi {

    @Autowired
    private IVideoService videoService;

    @Autowired
    UserSupport userSupport;

//    @Autowired
//    private ElasticSearchService elasticSearchService;

    /**
     * 视频投稿
     *
     * @param video
     * @return
     */
    @PostMapping("videos")
    public JsonResponse<String> addVideos(@RequestBody Video video) {
        Long userId = userSupport.getCurrentUserId();
        video.setUserId(userId);
        videoService.addVideos(video);
//        elasticSearchService.addVideo(video);
        return JsonResponse.success();
    }

    /**
     * 分页请求视频列表
     *
     * @param size
     * @param no
     * @param area
     * @return
     */
    @GetMapping("videos")
    public JsonResponse<Page<Video>> pageListVideos(Integer size, Integer no, String area) {
        Page<Video> page = videoService.pageListVideos(size, no, area);
        return new JsonResponse<>(page);
    }

    /**
     * 视频分片
     *
     * @param request
     * @param response
     * @param url
     * @throws Exception
     */
    @GetMapping("/video-slices")
    public void viewVideoOnlineBySlices(HttpServletRequest request,
                                        HttpServletResponse response,
                                        String url) throws Exception {
        videoService.viewVideoOnlineBySlices(request, response, url);
    }

    @PostMapping("/video-likes")
    public JsonResponse<String> addVideoLikes(@RequestParam Long videoId) {
        videoService.addVideoLikes(userSupport.getCurrentUserId(), videoId);
        return JsonResponse.success();
    }

    @DeleteMapping("/video-likes")
    public JsonResponse<String> deleteVideoLike(@RequestParam Long videoId) {
        videoService.deleteVideoLikes(userSupport.getCurrentUserId(), videoId);
        return JsonResponse.success();
    }

    /**
     * 查询视频点赞数量
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-likes")
    public JsonResponse<Map<String, Object>> getVideoLikes(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> map = videoService.getVideoLikes(videoId, userId);
        return new JsonResponse<>(map);
    }

    /**
     * 收藏视频
     *
     * @param videoCollection
     * @return
     */
    @PostMapping("/video-collection")
    public JsonResponse<String> addVideoCollection(@Validated @RequestBody VideoCollection videoCollection) {
        Long userId = userSupport.getCurrentUserId();
        videoCollection.setUserId(userId);
        videoService.addVideoCollection(videoCollection);
        return JsonResponse.success();
    }

    /**
     * 取消收藏
     *
     * @param videoId
     * @return
     */
    @DeleteMapping("video-collection")
    public JsonResponse<String> deleteVideoCollection(@RequestParam Long videoId) {
        Long userId = userSupport.getCurrentUserId();
        videoService.deleteVideoCollection(userId, videoId);
        return JsonResponse.success();
    }

    /**
     * 查询视频收藏数量
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-collections")
    public JsonResponse<Map<String, Object>> getVideoCollections(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = videoService.getVideoCollections(userId, videoId);
        return new JsonResponse<>(result);
    }
    // 收藏分组，新增、删除、查询

    /**
     * 用户投币
     *
     * @param videoCoin
     * @return
     */
    @PostMapping("/video-coins")
    public JsonResponse<String> addVideoCoins(@Validated @RequestBody VideoCoin videoCoin) {
        Long userId = userSupport.getCurrentUserId();
        videoService.addVideoCoins(videoCoin, userId);
        return JsonResponse.success();
    }

    /**
     * 查询视频投币数量
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-coins")
    public JsonResponse<Map<String, Object>> getVideoCoins(@RequestParam Long videoId) {
        Long userId = null;
        try {
            userId = userSupport.getCurrentUserId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = videoService.getVideoCoins(videoId, userId);
        return new JsonResponse<>(result);
    }

    /**
     * 视频添加评论
     *
     * @param videoComment
     * @return
     */
    @PostMapping("/videos-comments")
    public JsonResponse<String> addVideoComment(@Validated @RequestBody VideoComment videoComment) {
        Long userId = userSupport.getCurrentUserId();
        videoComment.setUserId(userId);
        videoService.addVideoComment(videoComment);
        return JsonResponse.success();
    }

    /**
     * 分页查询视频评论
     *
     * 赞、踩
     * @param size
     * @param no
     * @param userId
     * @return
     */
    @GetMapping("/video-comments")
    public JsonResponse<Page<VideoComment>> pageListVideoComments(@RequestParam Integer size,
                                                    @RequestParam Integer no,
                                                    @RequestParam Long videoId) {
        Page<VideoComment> page = videoService.pageListVideoComments(size, no, videoId);
        return new JsonResponse<>(page);
    }

    /**
     * 获取视频详细信息
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-details")
    public JsonResponse<Map<String, Object>> getVideoDetails(Long videoId) {
        Map<String, Object> result = videoService.getVideoDetails(videoId);
        return new JsonResponse<>(result);
    }

    /**
     * 添加视频观看记录
     * 用户登录情况下观看
     * 用户未登录情况下查看
     * @param videoView
     * @param request
     * @return
     */
    @PostMapping("/video-views")
    public JsonResponse<String> addView(@RequestBody VideoView videoView,
                                        HttpServletRequest request) {
        Long userId;
        try {
            userId = userSupport.getCurrentUserId();
            videoView.setUserId(userId);
            videoService.addVideoView(videoView, request);
        } catch (Exception e) {
            videoService.addVideoView(videoView, request);
        }
        return JsonResponse.success();
    }

    /**
     * 查询视频播放量
     *
     * @param videoId
     * @return
     */
    @GetMapping("/video-view-counts")
    public JsonResponse<Long> getVideoViewCounts(@RequestParam Long videoId) {
        Long count = videoService.getVideoViewCounts(videoId);
        return new JsonResponse<Long>(count);
    }


    /**
     * 视频内容推荐
     *
     * @return
     */
    @GetMapping("/recommend")
    public JsonResponse<List<Video>> recommend() throws TasteException {
        Long userId = userSupport.getCurrentUserId();
        List<Video> list = videoService.recommend(userId);
        return new JsonResponse<>(list);
    }


    /**
     * 视频帧截取生成黑白剪影
     *
     * @param videoId
     * @param fileMd5
     * @return
     */
    @GetMapping("/video-frames")
    public JsonResponse<List<VideoBinaryPicture>> captureVideoFrame(@RequestParam Long videoId,
                                                                    @RequestParam String fileMd5) throws Exception {
        List<VideoBinaryPicture> list = videoService.convertVideoToImage(videoId, fileMd5);
        return new JsonResponse<>(list);
    }

}